package com.photoeditor.demo.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import com.common.base.ComponentContext;

/**
 * Created by Administrator on 2017/5/20 0020.
 */

public class AppOpenUtil {

    private Context mContext;
    private final static String INSTAGRAM = "ins";
    private final static String TWITTER = "twi";
    private final static String FACEBOOK = "fb";
    private final static String YOUTUBE = "ytb";
    private final static String BROWSER = "url";
    private final static String GOOGLEPLAY = "gp";


    public AppOpenUtil(Context mContext) {
        this.mContext = mContext;
    }

    public void open(String originId) {
        if (TextUtils.isEmpty(originId)) {
            return;
        }
        String[] split = originId.split("@");
        String app = BROWSER;
        String id = null;
        if (split.length == 2) {
            app = split[0];
            id = split[1];
        } else {
            id = split[0];
        }
        if (INSTAGRAM.equals(app)) {
            toInstagram(id);
        } else if (TWITTER.equals(app)) {
            toTwitter(id);
        } else if (FACEBOOK.equals(app)) {
            toFacebook(id);
        } else if (YOUTUBE.equals(app)) {
            toYouTube(id);
        } else if (BROWSER.equals(app)) {
            toBrowser(id);
        } else if (GOOGLEPLAY.equals(app)) {
            toGPStoreOrBrowser(id);
        }

    }

    public void toFacebook(String id) {
        Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
        String facebookUrl = "https://www.facebook.com/" + id;
        try {
            int versionCode = mContext.getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
            if (versionCode >= 3002850) { //newer versions of fb app
                // 此链接跳转app后，打开的页面非作者home页面，直接使用浏览器打开
//                facebookUrl = "fb://facewebmodal/f?href=" + facebookUrl;
            } else { //older versions of fb app
                facebookUrl = "fb://page/" + id;
            }
        } catch (PackageManager.NameNotFoundException | RuntimeException e) {
            //normal web url
        }
        facebookIntent.setData(Uri.parse(facebookUrl));
        try {
            mContext.startActivity(facebookIntent);
        } catch (ActivityNotFoundException | SecurityException e) {
            Toast.makeText(mContext, "sorry,your mobile can't be supported", Toast.LENGTH_LONG).show();
        }
    }

    public void toInstagram(String id){
        try{
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/_u/" + id));
            intent.setPackage("com.instagram.android");
            mContext.startActivity(intent);
        }catch (Exception e){
            try {
                mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/" + id)));
            } catch (Exception e2) {

            }
        }

    }

    public void toTwitter(String id){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String url = "https://twitter.com/" + id;
        try {
            // get the Twitter app if possible
            mContext.getPackageManager().getPackageInfo("com.twitter.android", 0);
            url = "twitter://user?screen_name=" + id;
        } catch (Exception e) {
            // no Twitter app, revert to browser
        }
        intent.setData(Uri.parse(url));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException | SecurityException e) {
            Toast.makeText(mContext, "sorry,your mobile can't be supported", Toast.LENGTH_LONG).show();
        }
    }

    public void toYouTube(String id){
        //网页  app还没找到方式
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String url = "https://www.youtube.com/user/"+id;
        intent.setData(Uri.parse(url));
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException | SecurityException e) {
            Toast.makeText(mContext, "sorry,your mobile can't be supported", Toast.LENGTH_LONG).show();
        }
    }

    public void toBrowser(String url){
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException | SecurityException e) {
            Toast.makeText(mContext, "sorry,your mobile can't be supported", Toast.LENGTH_LONG).show();
        }
    }

    public void toGPStoreOrBrowser(String packageName) {
        if(TextUtils.isEmpty(packageName)) {
            packageName = ComponentContext.getPackageName();
        }
        // 跳转到应用市场
        Intent intent = new Intent();
        try {
            intent.setAction(Intent.ACTION_VIEW);
            String appAddr = "market://details?id=" + packageName;
            intent.setData(Uri.parse(appAddr));
            intent.setPackage("com.android.vending");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException | SecurityException e) {
            intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            String webAddr = "https://play.google.com/store/apps/details?id=" + packageName;
            intent.setData(Uri.parse(webAddr));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                mContext.startActivity(intent);
            } catch (ActivityNotFoundException | SecurityException e1) {
                Toast.makeText(mContext, "sorry,your mobile can't be supported", Toast.LENGTH_LONG).show();
            }
        }
    }
}
